package com.uwantsoft.business.facade;

import com.uwantsoft.business.facade.dto.address.FavoriteAddressesDto;
import com.uwantsoft.business.facade.dto.address.LatLngDto;
import com.uwantsoft.business.facade.dto.parking.PriceDto;
import com.uwantsoft.business.facade.dto.user.*;
import com.uwantsoft.business.service.DriverServiceImpl;
import com.uwantsoft.business.service.domain.addresses.FavoriteAddress;
import com.uwantsoft.business.service.domain.user.Driver;
import com.uwantsoft.business.service.domain.user.LicensePlate;
import com.uwantsoft.util.paging.DefaultPaginatedList;
import com.uwantsoft.util.paging.PagingListLoader;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @author LeiPeichen.
 */
@Component("driverFacade")
public class DriverFacadeImpl {

    @Resource(name="driverService")
    private DriverServiceImpl driverService;

    public void sendVerifyCode(String mobileNumber){
        driverService.sendVerifyCode(mobileNumber);
    }

    public void login(DriverLoginDto driverLoginDto){
        driverService.login(driverLoginDto);
    }

    public FavoriteAddressesDto createFavoriteAddress(FavoriteAddressesDto favoriteAddressesDto){
        FavoriteAddress favoriteAddress = FavoriteAddress.createFavoriteAddress(favoriteAddressesDto);
        FavoriteAddress createFavoriteAddress = driverService.createFavoriteAddress(favoriteAddress);
        return createFavoriteAddress.convert();
    }

    public List<FavoriteAddressesDto> obtainFavoriteAddresses(){
        List<FavoriteAddress> favoriteAddressList = driverService.obtainFavoriteAddresses();
        List<FavoriteAddressesDto> favoriteAddressesDtoList = new ArrayList<FavoriteAddressesDto>();
        for (FavoriteAddress favoriteAddress : favoriteAddressList){
            favoriteAddressesDtoList.add(favoriteAddress.convert());
        }
        return favoriteAddressesDtoList;
    }
    public void deleteFavoriteAddress(String guid) {
        driverService.deleteFavoriteAddress(guid);
    }

    public List<LicensePlateDto> obtainLicensePlates() {
        List<LicensePlate> licensePlates = driverService.obtainLicensePlates();
        List<LicensePlateDto> licensePlateDtos = new ArrayList<LicensePlateDto>();
        for (LicensePlate licensePlate : licensePlates){
            licensePlateDtos.add(licensePlate.convert());
        }
        return licensePlateDtos;
    }

    public LicensePlateDto createLicensePlate(LicensePlateDto licensePlateDto) {
        LicensePlate licensePlate = LicensePlate.createLicensePlate(licensePlateDto);
        driverService.createLicensePlate(licensePlate);
        return licensePlate.convert();
    }

    public void updatePriorityLicensePlate(String guid) {
        driverService.updatePriorityLicensePlate(guid);
    }

    public void deleteLicensePlate(String guid) {
        driverService.deleteLicensePlate(guid);
    }

    public List<ParkHistoryDto> obtainParkHistories() {
        List<ParkHistoryDto> parkHistoryDtoList = new ArrayList<ParkHistoryDto>();

        ParkHistoryDto parkHistoryDtoOne = new ParkHistoryDto();
        parkHistoryDtoOne.setGuid("c92d422c-78af-4189-8764-ead2a58fb125");
        parkHistoryDtoOne.setName("春熙路");
        parkHistoryDtoOne.setDataTime("3月23日 17:40");
        parkHistoryDtoOne.setRegion("成都市");
        parkHistoryDtoOne.setAddress("群光广场");
        LatLngDto latLngDtoOne = new LatLngDto();
        latLngDtoOne.setLongitude("104.083081");
        latLngDtoOne.setLatitude("30.660187");
        parkHistoryDtoOne.setLatLng(latLngDtoOne);
        parkHistoryDtoList.add(parkHistoryDtoOne);



        ParkHistoryDto parkHistoryDtoTwo = new ParkHistoryDto();
        parkHistoryDtoTwo.setGuid("f8c4bf07-7848-494e-b536-2669054aa89e");
        parkHistoryDtoTwo.setName("IFS国际金融中心停车场");
        parkHistoryDtoTwo.setDataTime("3月25日 13:55");
        parkHistoryDtoTwo.setRegion("成都市");
        parkHistoryDtoTwo.setAddress("春熙路22号");
        LatLngDto latLngDtoTwo = new LatLngDto();
        latLngDtoTwo.setLongitude("104.086919");
        latLngDtoTwo.setLatitude("30.657073");
        parkHistoryDtoTwo.setLatLng(latLngDtoTwo);

        parkHistoryDtoList.add(parkHistoryDtoTwo);
        return parkHistoryDtoList;
    }

    public OrderDto obtainOrder() {
        OrderDto orderDto = new OrderDto();
        orderDto.setGuid("1aad6e74-44b3-4c49-b4e5-d3dc37055270");
        orderDto.setName("春熙路");
        orderDto.setAddress("群光广场");
        orderDto.setSpecialty("CHEAPEST");
        PriceDto priceDto = new PriceDto();
        priceDto.setHours("3");
        priceDto.setCost("30");
        orderDto.setPrice(priceDto);
        orderDto.setWalktime("5");
        orderDto.setDistance("800");
        return orderDto;
    }

    public DefaultPaginatedList obtainDrivers(final DriverOverviewQueryCondition condition){
        return new DefaultPaginatedList(condition).loadData(new PagingListLoader() {
            @Override
            public List loadList() {
                List<DriverOverviewDto> overviewDtos = new ArrayList<DriverOverviewDto>();
                List<Driver> drivers = driverService.obtainDrivers(condition);
                for (Driver driver : drivers) {
                    overviewDtos.add(driver.convertOverviewDto());
                }
                return overviewDtos;
            }

            @Override
            public int loadFullListSize() {
                return driverService.obtainDriverTotalAmount(condition);
            }
        });
    }

    public void lockDriver(String guid, boolean lock) {
        driverService.lockDriver(guid,lock);
    }

    public DriverDetailDto obtainDriver(String guid) {
        Driver driver = driverService.obtainDriver(guid);
        return driver.convertDetailDto();
    }
}
